package com.example.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.test.entity.User;
import com.example.test.service.IUserService;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    private Object login(@RequestBody User user){
        User user1 = iUserService.login(user.getUsername(), user.getPassword());
        if(user1==null){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",500);
            jsonObject.put("msg","帐户或密码不正确。");
            return jsonObject;
        }
        else{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",200);
            jsonObject.put("msg","success");
            JSONObject data = new JSONObject();
            data.put("accessToken",user.getUsername()+"-accessToken");
            jsonObject.put("data",data);
            return jsonObject;
        }
    }

    @RequestMapping(value = "/userInfo", method = RequestMethod.POST)
    private Object userInfo(@RequestBody JSONObject object){
        String username = object.getString("accessToken").split("-")[0];
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","success");
        JSONObject data = new JSONObject();
        if(iUserService.getUser(username).getScreen()==0)
            data.put("permissions",new String[] {"admin","editor"});
        else
            data.put("permissions",new String[] {"editor"});
        data.put("username",username);
        data.put("avatar","https://i.gtimg.cn/club/item/face/img/8/15918_100.gif");
        jsonObject.put("data",data);
        return jsonObject;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    private Object logout(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("msg","success");
        return jsonObject;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    private List<User> list(){
        return iUserService.list();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    private void add(@RequestBody User user){
        iUserService.add(user.getUsername(),user.getPassword(),user.getScreen());
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    private void del(@RequestParam int id){
        iUserService.del(id);
    }

    @RequestMapping(value = "/update_pass", method = RequestMethod.POST)
    private void update_pass(@RequestBody User user){
        iUserService.update_pass(user.getId(),user.getPassword());
    }

    @RequestMapping(value = "/update_screen", method = RequestMethod.POST)
    private void update_screen(@RequestBody User user){
        iUserService.update_screen(user.getId(),user.getScreen());
    }
}
